<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script src="common.js"></script>
    <input type="button" class="btn" id="btn" value="点击随机选择" />
    <select id="city"></select>
    <script>
      //获取元素
      var btn = my$("btn");
      var city = my$("city");
      var opts = city.children;

      //模拟后台传输数据
      var datas = ["北京", "洛阳", "上海", "哈尔滨", "杭州", "广州", "深圳"];
      for (var i = 0; i < datas.length; i++) {
        var option = document.createElement("option");
        city.appendChild(option);
        option.innerText = datas[i];
      }

      btn.onclick = function () {
        var n = Math.floor(Math.random() * datas.length);
        opts[n].selected = true;
      };
    </script>
  </body>
</html>
